﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System.Web.Mvc;
using Huirui.Cavan.Domain.Seedwork;
using System.Collections.Generic;

namespace Huirui.Cavan.Presentation.Controller.StockModule
{
    public class StockInOutTypeController : EntityController<StockInOutType, int, StockInOutTypeSearch, IStockInOutTypeViewModel, IStockInOutTypeAppService>, IStockInOutTypeController
    {
        public override string ControllerName
        {
            get { return ControllerConst.STOCKINOUTTYPE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public StockInOutTypeController()
        {
            this.Model = ModelFactory.CreateModel<IStockInOutTypeViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        public ActionResult AddStockInOutType()
        {
            return View("~/Views/Stock/v1/addStockInOutType.cshtml");
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        public JsonResult AddStockInOutType(StockInOutTypeViewModel model)
        {
            BoolResult jsonResult = null;
            BoolGenericResult<StockInOutType> result = this.AppService.Edit(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        public JsonResult GetStockInOutTypes(int id)
        {
            StockInOutTypeJsonViewModel json = new StockInOutTypeJsonViewModel();
            json.rows = new List<StockInOutTypeJson>();

            IList<StockInOutType> stockInOutTypes = this.AppService.GetBySearch(SearchFactory.CreateStockInOutTypeSearch(id == 1 ? true : false)).ToList();
            foreach (StockInOutType siot in stockInOutTypes)
            {
                StockInOutTypeJson siotj = new StockInOutTypeJson();
                siotj.ID = siot.Id;
                siotj.Name = siot.Name;
                siotj.FromAccount = siot.FromAccount.Id;
                siotj.StrFromAccount = siot.FromAccount.Name;
                siotj.IsCostAccounting = siot.IsCostAccounting;
                siotj.IsIn = siot.IsIn;

                json.rows.Add(siotj);
            }
            json.total = stockInOutTypes.Count;

            return Json(json, "application/json");
        }
    }
}
